package com.ara.common.project.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.viewpager2.widget.ViewPager2;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.ara.common.kernel.base.BaseActivity;
import com.ara.common.kernel.util.GlideUtils;
import com.ara.common.project.R;
import com.ara.common.project.constant.XRouterApiConstant;
import com.ara.common.project.databinding.ActivityLargePictureViewBinding;
import com.ara.common.project.databinding.ItemLargePictureBinding;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.gyf.immersionbar.ImmersionBar;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Created by XieXin on 2020/7/9.
 * 大图查看
 */
@Route(path = XRouterApiConstant.API_LARGE_PICTURE_VIEW)
public class LargePictureViewActivity extends BaseActivity<ActivityLargePictureViewBinding> {
    private LargePictureAdapter adapter;

    @Autowired
    List<String> images;
    @Autowired
    int index;

    @SuppressLint("SetTextI18n")
    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        ARouter.getInstance().inject(this);
        ImmersionBar.with(this).statusBarColor(R.color.black).fitsSystemWindows(true).statusBarDarkFont(false).init();

        if (images == null || images.isEmpty()) finish();

        binding.ivClose.setOnClickListener(v -> finish());

        adapter = new LargePictureAdapter();
        binding.viewPager.setAdapter(adapter);
        adapter.setList(images);
        binding.viewPager.setCurrentItem(index);

        binding.tvNum.setVisibility(images.size() > 1 ? View.VISIBLE : View.GONE);

        binding.tvNum.setText((index + 1) + "/" + images.size());
        binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                binding.tvNum.setText((position + 1) + "/" + images.size());
            }
        });

    }

    public static class LargePictureAdapter extends BaseQuickAdapter<String, LargePictureAdapter.ViewHolder> {

        public LargePictureAdapter() {
            super(R.layout.item_large_picture);
        }

        @SuppressLint("SetTextI18n")
        @Override
        protected void convert(@NotNull ViewHolder holder, String item) {
            GlideUtils.loadImage(getContext(), item, holder.binding.photo);
        }

        static class ViewHolder extends BaseViewHolder {
            ItemLargePictureBinding binding;

            public ViewHolder(@NotNull View view) {
                super(view);
                binding = ItemLargePictureBinding.bind(view);
            }
        }
    }
}